package xainchenggaoji;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author Mr.ma
 * @since 2022/11/2 21:43
 * 线程池有：
 *      newFixedThreadExecutor:固定大小线程池
 *      newSingleThreadPool:单一线程池
 *      newCachedThreadPool:可变线程池
 *      newScheduledThreadPool:延迟线程池
 **/
public class CreatePool {
    public static void main(String[] args) {
        //创建单一的线程池
        ExecutorService executorService = Executors.newSingleThreadExecutor();

        for (int i = 0; i < 10; i++) {
            int a=i;
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName()+"===="+a);
                }
            });
        }
    }
}
class TestExecutorDemoOne{
    public static void main(String[] args) {

        ExecutorService executorService = Executors.newFixedThreadPool(3);
        for (int i = 0; i < 10; i++) {
            int a=i;
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName()+"====="+a);
                }
            });
        }
    }
}
class TestExecutorDemoTwo{
    public static void main(String[] args) {

        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i < 10; i++) {
            final  int a=i;
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName()+"========="+a);
                }
            });
        }
    }
}
class TestExecutorDemoThree{
    public static void main(String[] args) {

        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10);
        for (int i = 0; i < 10; i++) {
            final int a=i;
            scheduledExecutorService.schedule(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName()+"====="+a);
                }
            },10, TimeUnit.SECONDS);
        }
    }
}
